﻿using System;
using System.Collections;
using System.Web.UI.WebControls;
using Library.Util;

namespace Library.UserInterface.Chart
{
    public class GChart : Literal
    {
        private Enums.GChartType _chartType;
        private GChartColumnCollection _columns;
        private ArrayList _datas;
        private int _width;
        private int _height;

        public Enums.GChartType ChartType
        {
            get { return _chartType; }
            set { _chartType = value; }
        }

        public GChartColumnCollection Columns
        {
            get { return _columns; }
            set { _columns = value; }
        }

        public ArrayList Datas
        {
            get { return _datas; }
            set { _datas = value; }
        }

        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        public int Height
        {
            get { return _height; }
            set { _height = value; }
        }

        public GChart()
        {
            Columns = new GChartColumnCollection();
            Datas = new ArrayList();
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (!Page.ClientScript.IsClientScriptIncludeRegistered("GoogleChart"))
            {
                Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "GoogleChart", "http://www.google.com/jsapi");
                string script = @"
                try {
                    google.load('visualization', '1', { packages: ['linechart'] });
                    google.load('visualization', '1', { packages: ['columnchart'] });
                    google.load('visualization', '1', { packages: ['barchart'] });
                    google.load('visualization', '1', { packages: ['areachart'] });
                    google.load('visualization', '1', { packages: ['piechart'] });
                } catch (err) {
                    window.status = 'GoogleChart sunucusuna bağlanılamadı.';
                }
                ";
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "GoogleChartLoader", script, true);
            }
            base.OnPreRender(e);
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {                        
            string id = IdGenerator.GenerateGuid().Replace("-", "");
            writer.WriteLine("<script type=\"text/javascript\">");
            writer.Indent++;
            writer.WriteLine("google.setOnLoadCallback(drawChart" + id + ");");
            writer.WriteLine("function drawChart" + id + "() {");
            writer.Indent++;
            writer.WriteLine("var data = new google.visualization.DataTable();");
            for (int i = 0; i < Columns.Count; i++)
            {
                if (Columns[i].DataType == Enums.GChartColumnDataType.String)
                    writer.WriteLine("data.addColumn('string','" + Columns[i].Name + "');");
                if (Columns[i].DataType == Enums.GChartColumnDataType.Number)
                    writer.WriteLine("data.addColumn('number','" + Columns[i].Name + "');");
            }
            writer.WriteLine("data.addRows(" + Datas.Count + ")");
            for (int i = 0; i < Datas.Count; i++)
            {
                for (int j = 0; j < Datas[i].ToString().Split('|').Length; j++)
                {
                    if (Columns[j].DataType == Enums.GChartColumnDataType.String)
                        writer.WriteLine("data.setValue(" + i + ", " + j + ", '" + Datas[i].ToString().Split('|')[j] + "');");
                    if (Columns[j].DataType == Enums.GChartColumnDataType.Number)
                        writer.WriteLine("data.setValue(" + i + ", " + j + ", " + Datas[i].ToString().Split('|')[j] + ");");
                }
            }
            writer.Indent--;
            writer.WriteLine("var chart = new google.visualization." + ChartType + "Chart(document.getElementById('_chartDiv_" + id + "')); ");
            writer.WriteLine("chart.draw(data, { width: " + (Width - 10) + ", height: " + (Height - 10) + ", is3D: true, legend: 'bottom', title: '" + Text + "' });");
            writer.WriteLine("}");
            writer.Indent--;

            writer.WriteLine("</script>");
            writer.WriteLine("<div id=\"_chartDiv_" + id + "\" style=\"width: + " + Width + "px; height:" + Height + "px;\"></div>");

            Text = "";
            base.Render(writer);
        }
    }
}